package cn.itsource.controller;


import cn.itsource.basic.domain.BaseQuery;
import cn.itsource.basic.util.PageList;
import cn.itsource.domain.org.Department;
import cn.itsource.service.DepartmentService;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dept")
public class DepartmentController {


    @Autowired
    DepartmentService departmentService;

    /**
     * 1.请求url:.dept/findPage
     * 2.请求方式;post
     * 3.请求参数 :{ pageNum : '',pageSize:'',keyword:''}
     * 4.PageList
     */
    @PostMapping("/findPage")
    @Cacheable(cacheNames="dept",key="'dept1'")
    public PageList<Department> findPage(@RequestBody BaseQuery baseQuery){
        /**
         * 缓存步骤:
         * 1.先查询redis 是否有数据
         * 2.如果有直接返回结果,如果没有,查询数据库
         * 3.将数据从数据库查出存入redis
         * 4.返回结果到前端
         */
        //使用mybatisplus 完成分页的步骤
        //1,.创建page对象
        Page page = new Page<Department>();
        //当前页数
        //2.设置page对象的值
        page.setCurrent(baseQuery.getPage());
                //每页条数
        page.setSize(baseQuery.getPageSize());
        //3.完成分页查询
        Page selectPage = departmentService.selectPage(page);
        PageList pageList = new PageList<>(selectPage.getTotal(), selectPage.getRecords());
        return pageList;
    }
}
